var originRequest = require('request');
var cheerio = require('cheerio');
var iconv = require('iconv-lite');
var http = require('http');
var listModule = require('./deli-list');

var uptate = require('./mysql.test');
var headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36'
};

function request(url, callback) {
    var options = {
        url: url,
        encoding: null,
        //代理服务器
        //proxy: 'http://xxx.xxx.xxx.xxx:8888',
        headers: headers
    };
    originRequest(options, callback);
}
var errorList = [];
listModule.findList(0);
listModule.emitter.on('dataLoadFinished', (list) => {

    for (var index = 0; index < list.length; index++) {

        var element = list[index];
        var url = 'http://www.anluyun1688.com/Home/Product/' + element + '?date=' + Math.random() * (new Date().getTime() - element) + element;
        ((url) => {

            setTimeout(() => {

                request(url, function(err, res, body) {
                    if (body == null) {
                        console.log(err);
                        console.log(url);
                    }
                    if (body == null) return
                    var html = iconv.decode(body, 'utf-8');
                    var $ = cheerio.load(html, { decodeEntities: false });
                    var sql = 'update t_agr_prod_geo_indicat set area_names = ?,info = ?   where name = ?';
                    var condition = [];
                    $("a.ie-pie.rad50").each((index, item) => {

                        if (index == 0) condition[2] = $(item).text().trim(); //条件.name
                        if (index == 1) condition[0] = $(item).text().trim(); //地区
                    });

                    var content = $('.pub-content').html(); //内容
                    condition[1] = content;

                    uptate.updateDiliInfo(condition);
                });
            }, Math.random() * (10000));
        })(url);
    }

});